package com.huobao.yunying.adapters;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

import com.huobao.yunying.entity.ImageBanner;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.ImageUtils.OnImageLoadListener;
import com.huobao.yunying.utils.LinkTypeUtils;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import java.util.List;

public class ImageAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private Context context;
	private List<ImageBanner> list;
	private RelativeLayout layout;

	public ImageAdapter(Context context, List<ImageBanner> list, RelativeLayout layout) {
		inflater = LayoutInflater.from(context);
		this.context = context;
		this.layout = layout;
		this.list = list;
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int position) {
		return list.get(position % list.size());
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.image_item, null);
		}
		ImageView imgView = (ImageView) convertView.findViewById(R.id.imgView);
		ImageUtils.loadimg(imgView, list.get(position % list.size()).getAd_logo(), new OnImageLoadListener() {

			@Override
			public void onLoadSuccess(Drawable drawable) {
				int newHight = ScreenUtil.getInstant().getDisWidth(context) * drawable.getIntrinsicHeight()
						/ drawable.getIntrinsicWidth();
				LayoutParams params = (LayoutParams) layout.getLayoutParams();
				params.width = ScreenUtil.getInstant().getDisWidth(context);
				params.height = newHight;
				layout.setLayoutParams(params);
			}
		});
		imgView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ImageBanner banner = list.get(position % list.size());
				LinkTypeUtils util = new LinkTypeUtils(context, banner.getAd_id(), banner.getAd_link(), banner.getAd_logo(), banner.getAd_name(), banner.getLink_type(), banner.getLink_id(), banner.getSpec_id());
				util.skip(false);
			}
		});
		return convertView;
	}
}
